home *** CD-ROM | disk | FTP | other *** search
- /*
- TransSkel multiple-window demonstration: ZoomRect module
-
- This module handles a window in which successive randomly generated
- rectangles are smoothly interpolated into one another. The display
- is white on black, which results in some interesting problems (see
- ZDrawGrowBox, for instance). The display adjusts itself to the size
- of the window, so that the zoom series always lie entirely within
- the window. Clicking the mouse in the window pauses the display until
- the button is released.
-
- 14 June 1986 Paul DuBois
- */
-
- # include "MultiSkel.h"
-
-
- # define zoomSteps 15 /* # rects in interpolative series */
-
- WindowPtr zoomWind;
- static Rect zRect[zoomSteps]; /* set of interpolated rectangles */
- static Rect zSrcRect;
- static int sizeX; /* size of window in pixels */
- static int sizeY;
-
-
- SetZoomSize ()
- {
- Rect r;
-
- r = zoomWind->portRect;
- r.right -= 15; /* don't use right edge */
- sizeX = r.right;
- sizeY = r.bottom;
- }
-
-
- /*
- return integer between zero and max (inclusive). assumes max is
- non-negative.
- */
-
- Rand (max)
- int max;
- {
- register int t;
-
- t = Random ();
- if (t < 0) t = -t;
- return (t % (max + 1));
- };
-
-
- /*
- Interpolate one rectangle smoothly into another. Erase the previous
- series as the new one is drawn.
- */
-
- ZoomRect (r1, r2)
- Rect r1, r2;
-
- {
- register int r1left, r1top;
- register int l, t;
- register int j;
- int hDiff, vDiff, widDiff, htDiff;
- int r, b;
- int rWid, rHt;
-
-
- r1left = r1.left;
- r1top = r1.top;
- hDiff = r2.left - r1left; /* positive if moving to right */
- vDiff = r2.top - r1top; /* positive if moving down */
- rWid = r1.right - r1left;
- rHt = r1.bottom - r1top;
- widDiff = (r2.right - r2.left) - rWid;
- htDiff = (r2.bottom - r2.top) - rHt;
- /*
- order of evaluation is important in the rect coordinate calculations.
- since all arithmetic is integer, you can't save time by calculating
- j/zoomSteps and using that - it'll usually be zero.
- */
- for (j = 1; j <= zoomSteps; j++)
- {
- FrameRect (&zRect[j-1]); /* erase a rectangle */
- l = r1left + (hDiff * j) / zoomSteps;
- t = r1top + (vDiff * j) / zoomSteps;
- r = l + rWid + (widDiff * j) / zoomSteps;
- b = t + rHt + (htDiff * j) / zoomSteps;
- SetRect (&zRect[j-1], l, t, r, b);
- FrameRect (&zRect[j-1]);
- }
- }
-
-
- Idle ()
- {
- int i;
- Point pt1, pt2;
- Rect dstRect;
-
- SetPt (&pt1, Rand (sizeX), Rand (sizeY)); /* generate new rect */
- SetPt (&pt2, Rand (sizeX), Rand (sizeY)); /* and zoom to it */
- Pt2Rect (pt1, pt2, &dstRect);
- SetWindClip (zoomWind); /* don't draw in right edge */
- ZoomRect (zSrcRect, dstRect);
- ResetWindClip ();
- zSrcRect = dstRect;
- }
-
-
- /*
- just pause zoom display while mouse down
- */
-
- static Mouse (thePt, t, mods)
- Point thePt;
- long t;
- int mods;
-
- {
- while (StillDown ()) { /* wait until mouse button released */ }
- }
-
-
- /*
- Draw the grow box in white on black. This is tricky: if the window
- is inactive, the grow box will be drawn black, as it should be. But
- if the window is active, the box will STILL be drawn black on white!
- So have to check whether the window is active or not. The test for
- active has to be done carefully: the window manager stores 255 and 0
- for true and false, not real boolean values.
- */
-
- ZDrawGrowBox ()
- {
- Rect r;
-
- PenMode (notPatCopy);
- DrawGrowBox (zoomWind);
- PenMode (patXor);
- if ( ((WindowPeek) zoomWind)->hilited) /* grow box draw in white */
- { /* no matter what if active */
- r = zoomWind->portRect; /* - invert to fix */
- r.left = r.right - 14;
- r.top = r.bottom - 14;
- InvertRect (&r);
- }
- }
-
-
- static Update (resized)
- Boolean resized;
- {
- int i;
-
- EraseRect (&zoomWind->portRect);
- ZDrawGrowBox (zoomWind);
- SetWindClip (zoomWind);
- for (i = 0; i < zoomSteps; ++i)
- FrameRect (&zRect[i]);
- ResetWindClip (zoomWind);
- if (resized)
- SetZoomSize (); /* adjust to new window size */
- }
-
-
- static Activate (active)
- Boolean active;
- {
-
- ZDrawGrowBox (zoomWind);
- if (active)
- DisableItem (editMenu, 0);
- else
- EnableItem (editMenu, 0);
- DrawMenuBar ();
- }
-
-
- static Halt ()
- {
- CloseWindow (zoomWind);
- }
-
-
- ZoomWindInit ()
- {
- int i;
-
- zoomWind = GetNewWindow (zoomWindRes, nil, -1L);
- SkelWindow (zoomWind,
- Mouse, /* pause while button down */
- nil, /* ignore key clicks */
- Update,
- Activate,
- nil, /* no close proc */
- Halt, /* when done with window */
- Idle, /* draw a new series */
- true); /* run only when frontmost */
-
- SetZoomSize ();
- BackPat (&black);
- PenMode (patXor);
- SetRect (&zSrcRect, 0, 0, 0, 0);
- for (i = 0; i < zoomSteps; ++i) /* initialize rect array */
- zRect[i] = zSrcRect;
- }
-